const webpack = require('webpack')
const path = require('path')
const proxyConfig = require('./config/proxyConfig.js')
const urlConfig = require('./config/config.js')
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin')
const CompressionPlugin = require('compression-webpack-plugin')
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')

function resolve(dir) {
  return path.join(__dirname, dir)
}

module.exports = {
  publicPath: './',
  productionSourceMap: false,
  
  lintOnSave: false,
  configureWebpack: (config) => {
    config.devtool = 'none';
    if (process.env.NODE_ENV == 'production') {
      config.plugins.forEach(val => {
        if (val.__pluginName === 'html') {
          val.options.configUrl = './config.js?v=' + Date.now()
        }
        if (val.__pluginName === 'copy') {
          val.patterns = [{
            from: path.resolve(__dirname, 'public'),
            to: path.resolve(__dirname, 'dist'),
            toType: 'dir',
            ignore: ['docs/*']
          }]
        }
      })
      config.plugins.push(
        new CompressionPlugin({
          algorithm: 'gzip', // 使用gzip压缩
          test: /\.js$|\.html$|\.css$/, // 匹配文件名
          filename: '[path][base].gz', // 压缩后的文件名(保持原文件名，后缀加.gz)
          minRatio: 0.8, // 压缩率小于1才会压缩
          threshold: 10240, // 对超过10k的数据压缩
          deleteOriginalAssets: false // 是否删除未压缩的源文件
        })
      )
    }

    // config.plugins.push(new BundleAnalyzerPlugin())

    config.plugins.push(
      new webpack.DefinePlugin({
        APP: {
          CONFIG: JSON.stringify(urlConfig[process.env.NODE_ENV])
        }
      })
    )
    config.plugins.push(
      new MonacoWebpackPlugin({
        // available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
        languages: ['javascript', 'css', 'html', 'typescript', 'json']
      })
    )
    return {
      resolve: {
        alias: {
          '@root': resolve('./'),
          '@': resolve('./src'),
          '@lang': resolve('./src/lang'),
          '@api': resolve('./src/api'),
          '@dist': resolve('./dist'),
          'langCustom': resolve('./src/lang'),
          'appConfig': resolve('./config/config.js')
        }
      }
    }
  },
  chainWebpack: config => {
    config.plugin('loadshReplace').use(new LodashModuleReplacementPlugin())

    config.optimization.splitChunks({
      chunks: 'all',
      minSize: 30 * 1000,
      maxInitialRequests: 8,
      name: true,
      cacheGroups: {
        ripples: {
          name: 'chunk-ripples',
          test: /[\\/]node_modules[\\/](ripples|@ripples)/,
          priority: -10,
          reuseExistingChunk: true
        },
        vendors: {
          name: 'chunk-vendors',
          test: /[\\/]node_modules[\\/]/,
          priority: -20,
          reuseExistingChunk: true
        }
      }
    })
  },
  devServer: {
    // 应用域名，须与平台应用管理配置域名保持一致
    // host: 'ripplescloud-dev.dadp-pres.crcloud.com', // 预发开发
    // host: 'ripples.crp-sit.crcloud.com', // 电力sit 
    host: 'console-trip.test.crdigital.com.cn', // sit
    // 若使用平台单点登录，端口必须为443或80
    port: 80,
    open: true,
    historyApiFallback: true,
    noInfo: true,
    disableHostCheck: true,
    proxy: proxyConfig,
    headers:{
      "Access-Control-Allow-Origin":"*"
    }
  },
  css: {
    loaderOptions: {
      // 向 CSS 相关的 loader 传递选项
      less: {
        // 重置主题变量
        // modifyVars: {},
        javascriptEnabled: true
      }
    }
  },
  pluginOptions: {
    mock: {
      entry: './mock/index.js',
      disable: true // 生产环境禁用mock,设置为true禁用mock
    }
  }
}
